package com.xz.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xz.R;
import com.xz.entity.Category;
import com.xz.entity.category.OneCategory;
import com.xz.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * <p>
 * 课程科目 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-08-08
 */
@RestController
@RequestMapping("/video/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /*
     文件上传
     */
    @PostMapping("save")
    public R getFile(MultipartFile file) {
        //上传过来excel文件
        categoryService.saveSubject(file, categoryService);
        return R.ok();
    }

    /*
     得到所有分类
     */
    @GetMapping("getAllCategory")
    public R getAllCategory(){
        List<OneCategory> list=categoryService.getAllOneTwoSubject();
        return R.ok().data("list",list);
    }
    /*
     添加一级分类
     */
    @PostMapping("addOneCategory")
    public R addOneCategory(@RequestBody Category category){
        category.setParentId("0");
        boolean flag = categoryService.save(category);
        if (flag) return R.ok();
        else return R.error();
    }
    /*
        添加二级分类
        */
    @PostMapping("addTwoCategory/{id}")
    public R addTwoCategory(@RequestBody Category category,@PathVariable String id){
        category.setParentId(id);
        boolean flag = categoryService.save(category);
        if (flag) return R.ok();
        else return R.error();
    }

    @DeleteMapping("{id}")
    public  R deleteMember(@PathVariable String id){
        boolean  flag= categoryService.removeById(id);
        if(flag) {
            return R.ok();
        } else {
            return R.error();
        }
    }
}

